package org.keshe.controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.keshe.CurrentUser;
import org.keshe.data.StuData;
import org.keshe.data.StuDataWithButton;
import org.keshe.data.StuDataWithInfo;
import org.keshe.user.NormalUser;

import java.net.URL;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
import java.util.ResourceBundle;

public class AbnormalInOut implements Initializable {

    private NormalUser normalUser;

    @FXML
    public TableView<StuDataWithInfo> table;
    @FXML
    private TableColumn<StuDataWithInfo, String> s_id;

    @FXML
    private TableColumn<StuDataWithInfo, String> sname;

    @FXML
    private TableColumn<StuDataWithInfo, String> sex;

    @FXML
    private TableColumn<StuDataWithInfo, String> major;

    @FXML
    private TableColumn<StuDataWithInfo, String> bedName;

    @FXML
    private TableColumn<StuDataWithInfo, String> info;

    @FXML
    void refreshClick(ActionEvent event) {
        table.getItems().clear();
        try {
            List<StuData> notReturnStu = normalUser.getNotReturnStu(LocalDate.now().minusDays(1));
            List<StuData> noInOutStu = normalUser.getNoInOutStu(LocalDate.now().minusDays(1));
            addTableContent(noInOutStu, "未出入");
            addTableContent(notReturnStu, "未归");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public void addTableContent(List<StuData> list, String info){
        ObservableList<StuDataWithInfo> observableList = FXCollections.observableArrayList();
        for(StuData stuData : list){
            StuDataWithInfo stuDataWithInfo = new StuDataWithInfo(stuData);
            stuDataWithInfo.setInfo(info);
            observableList.add(stuDataWithInfo);
        }
        table.getItems().addAll(observableList);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        if(CurrentUser.USER instanceof NormalUser){
            normalUser = (NormalUser) CurrentUser.USER;
        }else{
            throw new RuntimeException();
        }

        s_id.setCellValueFactory(new PropertyValueFactory<>("s_id"));
        sname.setCellValueFactory(new PropertyValueFactory<>("sname"));
        sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
        major.setCellValueFactory(new PropertyValueFactory<>("major"));
        bedName.setCellValueFactory(new PropertyValueFactory<>("bedName"));
        info.setCellValueFactory(new PropertyValueFactory<>("info"));
    }
}
